package com.example.newjiesai.util;
import com.alibaba.fastjson.JSONObject;

public class BaiduAPI {


    //访问应用token
    static String ak = "fisn0WtjfGWEqIGOlN9r6s9K0o1Z9q0G";
   static String ak2="Z1HZ8HG75ILg4u1533Ir5zNAEk0sQugp";
   static String ak3="AIhV6xBIxkfa50HDINU8yTOg7Qe3buQm";
    static String ak4="FiqxRV3X940ggohFiE1kz1ZTmj4LBfHQ";       //自己的
    /**
     * 逆地理编码
     * @param lat
     * @param lng
     * @return 位置描述信息
     */
    static public String getLocName(String lat,String lng){
        String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak="+ak3+"&output=json&location="+lat+","+lng;
        try {
            System.out.println(url);
            JSONObject locJSON = HttpUtil.httpRequestJSON(url,"GET");
            //JSONObject j=locJSON.getJSONObject("result").getJSONObject("addressComponent");
            //return j.getString("province")+j.getString("city")+j.getString("district")+j.getString("town")+j.getString("street")+j.getString("street_number");
             return  locJSON.getJSONObject("result").getString("formatted_address");
        }catch (Exception e){
            return "Fail Getting Locname";
        }

    }

    /**
     * IP定位
     */
    public String getLocByIP(String ip){
        String url = "http://api.map.baidu.com/location/ip?ip="+ip+"&ak="+ak;
        JSONObject locJSON = HttpUtil.httpRequestJSON(url,"GET");
        return locJSON.getString("content");
    }
    static public JSONObject getLocName2(String lat,String lng)

    {
        String url="http://api.map.baidu.com/geoconv/v1/?coords="+lng+","+lat+"&from=1&to=5&output=json&ak="+ak3;

           JSONObject locJSON = HttpUtil.httpRequestJSON(url,"GET");
          // System.out.println(locJSON.toJSONString());
          // System.out.println(locJSON.getString("status"));
           //System.out.println(locJSON.getString("result"));
           String loc=locJSON.getString("result");
           loc=loc.substring(1,loc.length()-1);
               JSONObject j=JSONObject.parseObject(loc);

               //return  getLocName(j.getString("y"),j.getString("x"));

      return  j;

    }


}
